package nc.bs.gl.plugin;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.pub.util.BaseDaoUtil;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.am.common.util.CollectionUtils;
import nc.vo.pub.BusinessException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class ProjectNamePlugin implements IBackgroundWorkPlugin {

    private BaseDAO baseDAO;

    public BaseDAO getBaseDAO() {
        return baseDAO == null ? new BaseDAO() : baseDAO;
    }

    @Override
    public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException {
        List<Map<String, Object>> List =  queryqm();
        for(Map qm:List){
            BaseDaoUtil.getInstance().executeUpdate("update bd_defdoc set shortname = " + qm.get("qm") + " where pk_defdoc = '" + qm.get("pk") + "'");
        }
        return null;
    }

    public List<Map<String, Object>> queryqm() throws BusinessException {
        //获取当年年和月
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义日期格式化器，使月份为两位数
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // 格式化当前日期以获取年和月
        String formattedDate = currentDate.format(formatter);

        // 拆分字符串以分别获取年和月
        String year = formattedDate.substring(0, 4);
        String month = formattedDate.substring(5, 7);
        String sql="SELECT pk, SUM(qm) AS qm\n" +
                "FROM (\n" +
                "      SELECT \n" +
                "        gl_docfree1.F24 AS pk,\n" +
                "        (SUM(CASE WHEN year = '"+year+"' AND adjustperiod = '00' THEN creditamount ELSE 0 END) -" +
                "         SUM(CASE WHEN year = '"+year+"' AND adjustperiod = '00' THEN debitamount ELSE 0 END) ) AS qm\n" +
                "     FROM \n" +
                "        gl_balance gl_balan\n" +
                "        INNER JOIN bd_accasoa ON gl_balan.pk_accasoa = bd_accasoa.pk_accasoa\n" +
                "        INNER JOIN bd_account ON bd_accasoa.pk_account = bd_account.pk_account\n" +
                "        INNER JOIN gl_docfree1 ON gl_balan.assid = gl_docfree1.assid\n" +
                "    WHERE \n" +
                "        gl_balan.pk_accountingbook = '1001A210000000000R84'\n" +
                "        AND gl_balan.voucherkind <> 5\n" +
                "        AND gl_balan.voucherkind <> 4\n" +
                "        AND gl_docfree1.F24 <> 'NN/A'\n" +
                "        AND gl_balan.dr = 0\n" +
                "        AND gl_balan.year = '"+year+"'\n" +

                "    GROUP BY \n" +
                "        gl_docfree1.F24\n" +
                " \n" +
                "    UNION ALL\n" +
                " \n" +
                "    SELECT \n" +
                "        gl_docfree1.F24 AS pk,\n" +
                "        (SUM(gl_detail.creditamount) - SUM(gl_detail.debitamount)) AS qm\n" +
                "    FROM \n" +
                "        gl_detail\n" +
                "        JOIN gl_docfree1 ON gl_detail.assid = gl_docfree1.assid\n" +
                "        JOIN bd_accasoa ON gl_detail.pk_accasoa = bd_accasoa.pk_accasoa\n" +
                "    WHERE \n" +
                "        gl_detail.yearv = '"+year+"'\n" +
                "        AND gl_detail.adjustperiod BETWEEN '01' AND '12'\n" +
                "        AND gl_detail.pk_accountingbook = '1001A210000000000R84'\n" +
                "        AND gl_detail.tempsaveflag <> 'Y'\n" +
                "        AND gl_detail.discardflagv <> 'Y'\n" +
                "        AND gl_detail.dr = 0\n" +
                "        AND gl_docfree1.F24 <> 'NN/A'\n" +
                "        AND gl_detail.voucherkindv NOT IN (2, 255, 4, 5)\n" +

                "    GROUP BY \n" +
                "        gl_docfree1.F24\n" +
                ") temp\n" +
                "GROUP BY pk\n" +
                "ORDER BY pk;";

        List<Map<String, Object>> sqlResult = (List<Map<String, Object>>) this.getBaseDAO().executeQuery(sql.toString(),
                new MapListProcessor());
        if (CollectionUtils.isEmpty(sqlResult)) {
            return null;
        }

        return sqlResult;
    }



}
